package lance.honeydew.action;

import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.UserMember;
import lance.honeydew.service.ShopService;
import lance.honeydew.util.UserMemberUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class HomeAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(HomeAction.class);

	/*Bean*/
	private ShopService shopService;
	private UserMember userMemberBo;
	private String loginEmail;
	private String loginPassword;
	private List<CategoryBig> categoryBigList;

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	
	public UserMember getUserMemberBo() {
		return userMemberBo;
	}

	public void setUserMemberBo(UserMember userMemberBo) {
		this.userMemberBo = userMemberBo;
	}
	
	public String getLoginEmail() {
		return loginEmail;
	}

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public List<CategoryBig> getCategoryBigList() {
		return categoryBigList;
	}

	public void setCategoryBigList(List<CategoryBig> categoryBigList) {
		this.categoryBigList = categoryBigList;
	}

	/*Action*/
	public String execute() {
		
		return SUCCESS;
	}
	
	/**/
	private String jsonMessage;

	public String getJsonMessage() {
		return jsonMessage;
	}

	public void setJsonMessage(String jsonMessage) {
		this.jsonMessage = jsonMessage;
	}
	
	/*Action*/
	/**
	 * 首頁
	 * @return
	 */
	public String welcome() {
		
		try {
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "welcome";
	}
	
	/**
	 * 確認session user
	 * @return
	 */
	public String checkSessionUser() {
		
		try {
			
			UserMember user = null;
			
			user = getSessionUserMember();
			
			if(user != null) {
				jsonMessage = "true";
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 登入 User
	 * @return
	 */
	public String loginUser() {
		
		try {
			
			if (StringUtils.isNotEmpty(loginEmail)
					&& StringUtils.isNotEmpty(loginPassword)) {
				
				UserMember userObj = shopService.checkLoginUserMember(loginEmail, loginPassword);
				
				if (userObj != null){
					setSessionUserMember(userObj);
					setUserMemberBo(new UserMember());
					
					jsonMessage = "true";
				} else {
					jsonMessage = "false";
				}
				
			} else {
				jsonMessage = "false";
			}
			
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 登出 User
	 * @return
	 */
	public String logoutUser() {
		
		try {
			
			setSessionUserMember(null);
			UserMember userObj = getSessionUserMember();
			if (userObj == null) {
				setUserMemberBo(new UserMember());
				
				jsonMessage = "true";
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 加入會員
	 * @return
	 */
	public String addUser() {
		
		try {
			
			if (userMemberBo != null) {
				
				UserMember userObj = null;
				boolean result = false;
				
				if (StringUtils.isNotEmpty(userMemberBo.getEmail())) {
					userObj = shopService.findUserMemberByEmail(userMemberBo.getEmail());
				}
				
				if (userObj == null) {
					userMemberBo.setIdentity(UserMemberUtil.IDENTITY_GENERAL_INT);
					userMemberBo.setState(UserMemberUtil.STATE_USERMEMBER_ENABLE_INT);
					result = shopService.createUserMemberByObj(userMemberBo);
					jsonMessage = "true";
				} else {
					jsonMessage = "false";
				}				
				
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 載入大中分類所有連結
	 * @return
	 */
	public String loadCategoryLink() {
		
		try {
			
			categoryBigList = shopService.findAllCategoryBigList();
			
		} catch(Exception e){
			logger.debug(e);
		}
		
		return SUCCESS;
	}
}
